﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Common;
using System.Data.SqlClient;
using Model.Admin.Customer;

namespace OnlineFloralShop.Admin.Customer
{
    public partial class CustomerDetail : System.Web.UI.Page

    {
        //Model.Admin.Customer.Customer cus = new Model.Admin.Customer.Customer();
        Model.Admin.Customer.Customer cus = new Model.Admin.Customer.Customer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                cus.FillListView(listcus, cus.lisCus());


            }
        }

        protected void btdelete_click(object sender, EventArgs e)
        {

            for (int i = 0; i < listcus.Items.Count; i++)
            {
                if (((CheckBox)listcus.Items[i].FindControl("checked")).Checked == true)
                {
                    string id = ((Label)listcus.Items[i].FindControl("UserID")).Text;
                    bool check = ((CheckBox)listcus.Items[i].FindControl("check")).Checked;
                    cus.updateUser(id, check);

                }
            }

            cus.FillListView(listcus, cus.lisCus());



        }
        protected void btSearch_click(object sender, EventArgs e)
        {
            cus.FillListView(listcus, cus.SearchCusByName(this.txtsearch.Text));
        }
        public string getSearchsql()
        {
            string sql = " select aspnet_Users.UserId,aspnet_Users.UserName,aspnet_Users.LoweredUserName,aspnet_Membership.Email,aspnet_Membership.CreateDate,aspnet_Membership.IsApproved from aspnet_Users,aspnet_Membership where aspnet_Users.UserId = aspnet_Membership.UserId ";
            if (searchId.Text != "")
                //{
                sql += " and aspnet_Users.UserId like '" + searchId.Text + "'";
            if (searchUsername.Text != "") { sql += " and aspnet_Users.UserName like '" + searchUsername.Text + "'"; }
            if (searchname.Text != "") { sql += " and FullName like '" + searchname.Text + "'"; }
            if (searchAddress.Text != "") { sql += " and Address like ' " + searchAddress.Text + "'"; }
            if (searchmail.Text != "") { sql += " and aspnet_Membership.Email like ' " + searchmail.Text + "'"; }
            if (searchBirth.Text != "") { sql += " and DateOfBirth = '" + searchBirth.Text + "'"; }
            if (searchAddDate.Text != "") { sql += " and aspnet_Membership.CreateDate = ' " + searchAddDate.Text + "'"; }

            // }
            /*else  
            // {
               // if (searchUsername.Text != "")
               // {
                    sql += " UserName like ' " + searchUsername.Text +"'";
                    if (searchname.Text != "") { sql += " and FullName like '" + searchname.Text + "'"; }
                    if (searchAddress.Text != "") { sql += " and Address like ' " + searchAddress.Text + "'"; }
                    if (searchmail.Text != "") { sql += " and Email like ' " + searchmail.Text + "'"; }
                    if (searchBirth.Text != "") { sql += " and DateOfBirth = '" + searchBirth.Text + "'"; }
                    if (searchAddDate.Text != "") { sql += " and CreateDate = ' " + searchAddDate.Text + "'"; }
                }
                else if(searchname.Text!="")
                {
                    sql+= "FullName like '" + searchname.Text + "'";
                    if (searchAddress.Text != "") { sql += " and Address like ' " + searchAddress.Text + "'"; }
                    if (searchmail.Text != "") { sql += " and Email like ' " + searchmail.Text + "'"; }
                    if (searchBirth.Text != "") { sql += " and DateOfBirth = '" + searchBirth.Text + "'"; }
                    if (searchAddDate.Text != "") { sql += " and CreateDate = ' " + searchAddDate.Text + "'"; }
                }
                else if (searchAddress.Text != "")
                {
                    sql +=" Address like '"+ searchAddress.Text + "'";
                    if (searchmail.Text != "") { sql += " and Email like ' " + searchmail.Text + "'"; }
                    if (searchBirth.Text != "") { sql += " and DateOfBirth = '" + searchBirth.Text + "'"; }
                    if (searchAddDate.Text != "") { sql += " and CreateDate = ' " + searchAddDate.Text + "'"; }
                }
                else if (searchmail.Text != "")
                {
                    sql +=" Email like ' "+searchmail.Text + "'";
                    if (searchBirth.Text != "") { sql += " and DateOfBirth = '" + searchBirth.Text + "'"; }
                    if (searchAddDate.Text != "") { sql += " and CreateDate = ' " + searchAddDate.Text + "'"; }
                }
                else if (searchBirth.Text != "")
                {
                    sql +=" DateOfBirth = " +searchBirth.Text;
                    if (searchAddDate.Text != "") { sql += " and CreateDate = ' " + searchAddDate.Text + "'"; }
                }
                else if (searchAddDate.Text != "") { sql += " CreateDate = " + searchAddDate.Text; }
                 
             }*/

            return sql;

        }

        protected void btsearchfilter_click(object sender, EventArgs e)
        {
            cus.FillListView(listcus, cus.SearhCusByfilter(getSearchsql()));


        }
        protected void oncheck_all_change(object sender, EventArgs e)
        {
            if (((CheckBox)listcus.FindControl("checkAll")).Checked)
            {


                for (int i = 0; i < listcus.Items.Count; i++)
                {
                    ((CheckBox)listcus.Items[i].FindControl("checked")).Checked = true;
                }
            }
            else
                for (int i = 0; i < listcus.Items.Count; i++)
                {
                    ((CheckBox)listcus.Items[i].FindControl("checked")).Checked = false;
                }

        }
        protected void change_select_sort(object sender, EventArgs e)
        {


            listcus.Sort(DropDownList2.SelectedValue, SortDirection.Descending);
            Label1.Text = DropDownList2.SelectedValue;

        }
        protected void listcus_Sorting(object sender, ListViewSortEventArgs e)
        {
            e.SortDirection = SortDirection.Descending;
        }
    
       }

    }

